package cn.itcast.wanxinp2p.gateway.filter;

/**
 * @创建人： king xuan
 * @创建时间：2021/1/29
 * @描述：
 */

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class LoginFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return false;
    }

    @Override
    public Object run() throws ZuulException {
        //获取请求上下文，当请求进入Zuul，Zuul会给每个请求创建一个上下文对象
        RequestContext ctx = RequestContext.getCurrentContext();
        //获取request
        HttpServletRequest request = ctx.getRequest();
        //获取请求参数中的token
        String token = request.getParameter("access_token");
        //判断是否为空
        if(token==null||"".equals(token)) {
            //为空拦截
            ctx.setSendZuulResponse(false);
            //设置状态码，没有权限
            ctx.setResponseStatusCode(401);
        }
        return null;
    }
}
